import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
import {
  PrismaModuleOptions,
  PrismaOptionsFactory,
} from './prisma-options.interface';
import { REQUEST } from '@nestjs/core';

@Injectable()
export class PrismaService implements OnModuleInit, PrismaOptionsFactory {
  constructor(@Inject(REQUEST) private request: Request) {}
  createPrismaModuleOptions():
    | Promise<PrismaModuleOptions>
    | PrismaModuleOptions {
    const headers = this.request.headers;
    const tenantId = headers['x-tenant-id'] || 'default';
    console.log('2', 'tenantId', tenantId);
    if (tenantId === 'default1') {
      return { url: 'postgresql://pguser:example@47.119.133.230:5432/testdb' };
    } else if (tenantId === 'default2') {
      return { url: 'mysql://root:example@47.119.133.230:3306/testdb' };
    }
    return {};
  }
  async onModuleInit() {}
}
